package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {
    @Resource
    private OverflowListGoodsDao overflowListGoodsDao;
    @Resource
    private UserDao userDao;
    @Resource
    private OverflowListDao overflowListDao;
    @Resource
    private GoodsDao goodsDao;
    @Override
    public ServiceVO saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {
        overflowListDao.insertOverflowList(overflowList);

        Gson gson = new Gson();
        List<OverflowListGoods> overflowListGoodsList = gson.fromJson(overflowListGoodsStr,new TypeToken<List<OverflowListGoods>>(){}.getType());

        // 设置当前操作用户
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        overflowList.setUserId(currentUser.getUserId());


        // 保存商品报溢单商品信息
        overflowListGoodsList.stream().peek(overflowListGoods -> {
            overflowListGoods.setOverflowListId(overflowList.getOverflowListId());

            overflowListGoodsDao.insertOverflowListGoods(overflowListGoods);

            // 修改商品库存，状态
            Goods goods = goodsDao.selectById(overflowListGoods.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity()+overflowListGoods.getGoodsNum());
            goods.setState(2);
            goodsDao.updateById(goods);
        }).collect(Collectors.toList());

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> getList(String sTime, String eTime) {

        Map<String, Object> map = new HashMap<>();
        List<OverflowList> list = overflowListDao.selectOverflowList( sTime, eTime);
        list.stream().peek(overflowList -> {
            //设置当前用户
            User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
            overflowList.setUserId(currentUser.getUserId());
            overflowList.setTrueName(currentUser.getTrueName());
        }).collect(Collectors.toList());

        map.put("rows", list);

        return map;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoodsList = overflowListGoodsDao.selectOverflowListGoods(overflowListId);

        Map<String, Object> map = new HashMap<>();
        map.put("rows",overflowListGoodsList);

        return map;
    }
}
